﻿//-----------------------------------------------------------------------
// <copyright file="MetadataRegistration.cs" company="Noboru Project">
//      Copyright (c) Noboru Project. All rights reserved.
//
//      This source code is subject to terms and conditions of the New BSD License (BSD).
//      A copy of the license can be found in the License.html file at the root of this 
//      distribution. If you can not locate the New BSD License (BSD), please send an 
//      email to noboru_adm@live.com.
//      By using this source code in any fashion, you are agreeing to be bound by
//      the terms of the New BSD License (BSD) for Noboru.
//
//      You must not remove this notice, or any other, from this software.
// </copyright>
//-----------------------------------------------------------------------

using System;
using Microsoft.Windows.Design.Metadata;

namespace Noboru.Windows.Controls.VisualStudio.Design
{
    /// <include file='Documentation\MetadataRegistrationDocumentation.uex' path='Members/Member[@for="MetadataRegistration.Class"]/*' />
    public class MetadataRegistration : IRegisterMetadata
    {
        private static AttributeTable _customAttributes;
        private static bool _initialized;

        /// <include file='Documentation\MetadataRegistrationDocumentation.uex' path='Members/Member[@for="MetadataRegistration.CustomAttributes"]/*' />
        public static AttributeTable CustomAttributes
        {
            get
            {
                if (_customAttributes == null)
                {
                    _customAttributes = new CustomMetadataBuilder().CreateTable();
                }

                return _customAttributes;
            }
        }

        /// <include file='Documentation\MetadataRegistrationDocumentation.uex' path='Members/Member[@for="MetadataRegistration.Register"]/*' />
        public void Register()
        {
            if (!_initialized)
            {
                MetadataStore.AddAttributeTable(CustomAttributes);

                _initialized = true;
            }
        }

        /// <include file='Documentation\CustomMetadataBuilderDocumentation.uex' path='Members/Member[@for="CustomMetadataBuilder.Class"]/*' />
        private class CustomMetadataBuilder : AttributeTableBuilder
        {
            /// <include file='Documentation\CustomMetadataBuilderDocumentation.uex' path='Members/Member[@for="CustomMetadataBuilder.Constructor"]/*' />
            public CustomMetadataBuilder()
            {
                // TODO: Add Design time code here! 
            }

            private void AddTypeAttributes(Type type, params Attribute[] attribs)
            {
                this.AddCallback(type, builder => builder.AddCustomAttributes(attribs));
            }

            private void AddMemberAttributes(Type type, string memberName, params Attribute[] attribs)
            {
                this.AddCallback(type, builder => builder.AddCustomAttributes(memberName, attribs));
            }
        }
    }
}
